<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of financialClass
 *
 * @author nathaniel.stevens.ad
 */
class financialClass {

    public $startDate, $endDate, $cost, $duration, $fy; //start, end, cost, hours for a subtask
//arrays store a single value for each quarter in a fy
    public $curYearMilestones, $curYearDuration, $curYearDollars; //proposed
    public $actualMilestones, $actualDuration, $actualDollars;
    public $rebaselineMilestones, $rebaselineDuration, $rebaselineDollars;
    public $taskID;

    /**
     * 
     * @param type $startDate mysql format
     * @param type $endDate mysql format
     * @param type $cost cost of the subtask
     * @param type $hours hours of the subtask
     * @param type $fy fy of this subtask
     */
    function __construct($startDate, $endDate, $cost, $hours, $fy) {
        $this->startDate = $startDate;
        $this->endDate = $endDate;
        $this->cost = $cost;
        $this->duration = $hours;
        $this->fy = $fy;
    }

    function initialize() {
        $this->curYearMilestones = array();
        $this->curYearDuration = array();
        $this->curYearDollars = array();
        $this->fillHours($this->startDate, $this->endDate, $this->duration);
        $this->fillCost($this->startDate, $this->endDate, $this->cost);
    }

    function toString() {
        $output = "";
        for ($i = 0; $i < 4; $i++) {
            $q = $i + 1;
            $hours = $this->curYearDuration[$i];
            $dollars = $this->curYearDollars[$i];
            $output .= "----Q$q: H->$hours $$$->$dollars <br>";
        }
        return $output;
    }

    function initializeFillManually() {
        $this->curYearMilestones = array(0, 0, 0, 0);
        $this->curYearDuration = array(0, 0, 0, 0);
        $this->curYearDollars = array(0, 0, 0, 0);
        $this->actualDollars = array(0, 0, 0, 0);
        $this->actualDuration = array(0, 0, 0, 0);
        $this->actualMilestones = array(0, 0, 0, 0);
        $this->rebaselineMilestones = array(0, 0, 0, 0);
        $this->rebaselineDuration = array(0, 0, 0, 0);
        $this->rebaselineDollars = array(0, 0, 0, 0);
    }

    function fillHours($taskStartDate, $taskEndDate, $hours) {
        $this->curYearDuration = $this->getQuarterValues($taskStartDate, $taskEndDate, $hours, "hours");
    }

    function fillCost($taskStartDate, $taskEndDate, $cost) {
        $this->curYearDollars = $this->getQuarterValues($taskStartDate, $taskEndDate, $cost, "dollars");
    }

    function getQuarterValue($taskStartDate, $taskEndDate, $quarterStart, $quarterEnd, $taskTotalDays, $value) {

        //echo "Q:  $quarterStart $quarterEnd <br>";
        //echo "ST: $taskStartDate $taskEndDate<br>";
        //echo '<br>';
        //scenario 1 (outside of this Quarter)
        //This task starts after the quarter ends or quarter starts after task ends 
        if ($this->isDateAfterDate2($taskStartDate, $quarterEnd) || $this->isDateAfterDate2($quarterStart, $taskEndDate)) {
            //echo 'Outside of quarter <br>';
            return 0;
        }
        //scenario 2 (All inside this quarter)
        //Task starts after quarter starts and quarter ends after task ends 
        else if ($this->isDateAfterDate2($taskStartDate, $quarterStart) && $this->isDateAfterDate2($quarterEnd, $taskEndDate)) {
            //return the days between start and end of task
            //echo 'All inside of quarter ';
            //$qDays = $this->getDaysBetween($taskStartDate, $taskEndDate);
            //$qPercent = $qDays / $taskTotalDays;
            return $value;
            //echo "Total: $taskTotalDays Days: $qDays Percent: $qPercent Value: $qValue <br>";
            //return $qValue;
        }
        //scenario 3 (Starts before this quarter and ends after this quarter)
        //Quarter start after task start and task end after quarter end
        else if ($this->isDateAfterDate2($quarterStart, $taskStartDate) && $this->isDateAfterDate2($taskEndDate, $quarterEnd)) {
            //return the days between start and end of quarter
            //echo 'All of quarter ';
            $qDays = $this->getDaysBetween($quarterStart, $quarterEnd);
            $qPercent = $qDays / $taskTotalDays;
            $qValue = $qPercent * $value;
            //echo "Total: $taskTotalDays Days: $qDays Percent: $qPercent Value: $qValue <br>";
            return $qValue;
        }
        //scenario 4 (Starts before this quarter and ends during this quarter)
        //quarter start after task start
        else if ($this->isDateAfterDate2($quarterStart, $taskStartDate)) {
            //get difference between quarterStart and taskEnd
            //echo 'Starts before - ends in quarter ';
            $qDays = $this->getDaysBetween($quarterStart, $taskEndDate);
            $qPercent = $qDays / $taskTotalDays;
            $qValue = $qPercent * $value;
            //echo "Total: $taskTotalDays Days: $qDays Percent: $qPercent Value: $qValue <br>";
            return $qValue;
        }
        //scenario 5 (Starts during this quarter and ends after this quarter)
        else {
            //get difference between taskStart and quarterEnd
            //echo 'Starts in quarter - ends after quarter ';
            $qDays = $this->getDaysBetween($taskStartDate, $quarterEnd);
            $qPercent = $qDays / $taskTotalDays;
            $qValue = $qPercent * $value;
            //echo "Total: $taskTotalDays Days: $qDays Percent: $qPercent Value: $qValue <br>";
            return $qValue;
        }
    }

    function getQuarterValues($taskStartDate, $taskEndDate, $value, $type) {

        $array = array();
        $fy = $this->fy - 1;
        $taskTotalDays = $this->getDaysBetween($taskStartDate, $taskEndDate);

        $quarter1Start = $fy . '-10-01';
        $quarter1End = $fy . '-12-31';
        $q1Value = $this->getQuarterValue($taskStartDate, $taskEndDate, $quarter1Start, $quarter1End, $taskTotalDays, $value);
        //array_push($array, round($q1Value));
        if (strcasecmp($type, "hours") === 0) {
            array_push($array, round($q1Value));
        } else {
            array_push($array, $this->myRound($q1Value));
        }
        $quarter2Start = $fy + 1 . '-01-01';
        $quarter2End = $fy + 1 . '-03-31';
        $q2Value = $this->getQuarterValue($taskStartDate, $taskEndDate, $quarter2Start, $quarter2End, $taskTotalDays, $value);
        if (strcasecmp($type, "hours") === 0) {
            array_push($array, round($q2Value));
        } else {
            array_push($array, $this->myRound($q2Value));
        }
        $quarter3Start = $fy + 1 . '-04-01';
        $quarter3End = $fy + 1 . '-06-30';
        $q3Value = $this->getQuarterValue($taskStartDate, $taskEndDate, $quarter3Start, $quarter3End, $taskTotalDays, $value);
        if (strcasecmp($type, "hours") === 0) {
            array_push($array, round($q3Value));
        } else {
            array_push($array, $this->myRound($q3Value));
        }
        $quarter4Start = $fy + 1 . '-07-01';
        $quarter4End = $fy + 1 . '-09-31';
        $q4Value = $this->getQuarterValue($taskStartDate, $taskEndDate, $quarter4Start, $quarter4End, $taskTotalDays, $value);
        if (strcasecmp($type, "hours") === 0) {
            array_push($array, round($q4Value));
        } else {
            array_push($array, $this->myRound($q4Value));
        }
        return $array;
    }

    static function myRound($value) {
        $wholeValue = round($value);
        if (strlen($wholeValue) > 5) {
            return round(substr($wholeValue, 0, 4) * .1);
        } else if (strlen($wholeValue) > 4) {
            return round(substr($wholeValue, 0, 3) * .1);
        } else {
            return round(substr($wholeValue, 0, 2) * .1);
        }
    }

    function printArray($array) {
        for ($i = 0; $i < sizeof($array); $i++) {
            echo $array[$i] . '<br>';
        }
    }

    function getDaysBetween($start, $end) {
        //echo "Days Between $start $end <br>";
        return ceil(abs(strtotime($end) - strtotime($start)) / 86400);
    }

    /**
     * True if date is after date 2 otherwise false
     * @param type $date mysql format (YYYY-MM-DD)
     * @param type $date2 mysql format (YYYY-MM-DD)
     */
    function isDateAfterDate2($date, $date2) {
//        $dateTime = new DateTime($date);
//        $dateTime2 = new DateTime($date2);
        $dateTime = strtotime($date);
        $dateTime2 = strtotime($date2);
        //echo "Time  $dateTime <br>";
        //echo "Time2 $dateTime2 <br>";
        if ($dateTime >= $dateTime2) {
            //echo "true <br>";
            return true;
        } else {
            //echo "false <br>";
            return false;
        }
    }

    function fillDollars() {
        
    }

}

?>
